var WxParse = require("../../lib/wxParse/wxParse.js")
var util = require('../../utils/util.js');
var api = require('../../config/api.js');
var Data = require("../../utils/data.js");
var Base64util = require("../../utils/base64.js");
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    number: 1,
    date: util.formatDay(new Date()),
    start: util.formatDay(new Date()),
    customerInfo: {},
    date: '',
    tomorrow: '',
    dates: 1, winWidth: 0, winHeight:0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    that.setData({
      room: { id: options.id }
    });
    that.getData();
    app.init(options, that);
    /** 
  * 获取系统信息 
  */
    wx.getSystemInfo({

      success: function (res) {
        that.setData({
          winWidth: res.windowWidth,
          winHeight: res.windowHeight
        });
      }

    });
  },
  getData: function () {
    let that = this;
    util.request(api.HotelRoomDetail, { id: that.data.room.id }).then(function (res) {
      if (res.code === '200') {
        var total = 0;
        total = that.data.number * parseFloat(res.datas.room.price);
        that.setData({
          room: res.datas.room,
          images: res.datas.images,
          imageNumber: res.datas.images.length,
          total: total
        });

        wx.setNavigationBarTitle({
          //页面标题为路由参数
          title: res.datas.room.type
        })
        WxParse.wxParse('content', 'html', Base64util.Base64.decode(res.datas.room.info, "utf-8"), that, 5);
      }
    });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var startDate = this.data.startDate;
    var endDate = this.data.endDate;
    console.log(startDate);
    console.log(endDate);
    var date = Data.formatDate(new Date(), "yyyy-MM-dd");
    var tomorrow1 = new Date();
    tomorrow1.setDate((new Date()).getDate() + 1);
    var tomorrow = Data.formatDate(new Date(tomorrow1), "yyyy-MM-dd");
    if (startDate == null) {
      startDate = date;
      endDate = tomorrow;
    }
    var aDate, oDate1, oDate2, iDays
    aDate = endDate.split("-")
    oDate1 = new Date(aDate[1] + '-' + aDate[2] + '-' + aDate[0])    //转换为12-18-2006格式  
    aDate = startDate.split("-")
    oDate2 = new Date(aDate[1] + '-' + aDate[2] + '-' + aDate[0])
    iDays = parseInt(Math.abs(oDate1 - oDate2) / 1000 / 60 / 60 / 24)    //把相差的毫秒数转换为天数  
    let that = this;
    var total = that.data.number * that.data.room.price * iDays;
    this.setData({
      date: startDate,
      tomorrow: endDate,
      dates: iDays,
      total: total
    });

    app.getShareInfo(this);
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },
  fnAddNum: function () {
    var that = this;
    var number = that.data.number + 1;
    var total = that.data.room.price * number * that.data.dates;
    that.setData({
      number: number,
      total: total
    })
  },
  fnMinNum: function () {
    var that = this;
    if (that.data.number < 2) {
      that.setData({
        number: 1,
        total: that.data.room.price * that.data.dates
      })
      return;
    }
    var number = that.data.number - 1;
    var total = that.data.room.price * number * that.data.dates;
    that.setData({
      number: number,
      total: total
    })
  },
  bindDateChange: function (e) {
    var that = this;
    var value = e.detail.value;
    that.setData({
      date: value
    })
  },
  fnOrder: function () {
    var type = "hotel";
    var id = this.data.room.id;
    var startDate = "";
    var endDate = "";
    var url = "/pages/preOrder/hotelOrder/hotelinfo?type=" + type +
      "&id=" + id +
      "&startDate=" + startDate +
      "&endDate=" + endDate;
    wx.navigateTo({
      url: url,
    });

  },
  fnInput: function (e) {
    var that = this;
    console.log(e);
    var customerInfo = that.data.customerInfo;
    customerInfo[e.currentTarget.id] = e.detail.value;
    that.setData({
      customerInfo: customerInfo
    })
  },
  previewImage: function () {
    var that = this;
    wx.previewImage({
      current: 1, // 当前显示图片的http链接  
      urls: that.data.images// 需要预览的图片http链接列表  
    })
  },
  //事件处理函数
  bindViewTap: function () {
    var that = this;
    var startDate = that.data.date;
    var endDate = that.data.tomorrow;
    console.log(startDate);
    console.log(endDate);
    wx.navigateTo({
      url: '../calender/index?startDate=' + startDate + "&endDate=" + endDate
    })
  },
  fnToIndex: function () {
    wx.switchTab({
      url: '/pages/index/index-new',
    })
  },
  gotoHome: function () {
    wx.switchTab({
      url: '/pages/index/index',
    })
  }
})